package com.flycard.order.controller;


import com.flycard.order.Order;
import com.flycard.order.properties.OrderProperties;
import com.flycard.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/order")
@Slf4j
@RefreshScope
@RestController
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderProperties orderProperties;

    @GetMapping("/config")
    public String config()
    {
        return "order.timeout=" + orderProperties.getTimeout() +
                "  order.auto.confirm = " +orderProperties.getAutoConfirm() +
                "  order.db-url=" + orderProperties.getDbUrl();
    }



    @GetMapping("/create")
    public Order createOrder(@RequestParam("userId") long userId, @RequestParam("productId") long productId) {

        return orderService.createOrder(userId, productId);
    }


    @GetMapping("/secKill")
    public Order secKill(@RequestParam("userId") long userId, @RequestParam("productId") long productId) {
        Order order=orderService.createOrder(userId, productId);
        order.setId(Long.MAX_VALUE);
        return order;
    }

    @GetMapping("/writeDb")
    public String WriteDb(){
        return "writeDb";
    }
    @GetMapping("/readDb")
    public String readDb(){
        log.info("readDb");
        return "readDb";
    }
}
